package com.zhouhailin.shirospringbootdemo.controller;

import com.zhouhailin.shirospringbootdemo.entity.User;
import com.zhouhailin.shirospringbootdemo.service.UserService;
import com.zhouhailin.shirospringbootdemo.utils.VerifyCodeUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author zhouhailin
 * @date 2023/1/4 20:15
 */
@Controller
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    /**
     * 注册用户
     *
     * @param user 注册用户
     * @return
     */
    @RequestMapping("/register")
    public String register(User user) {
        try {
            userService.register(user);
            logger.info("注册用户成功！");
            return "redirect:/login.jsp";
        } catch (Exception e) {
            logger.error("注册用户异常!", e);
            return "redirect:/register.jsp";
        }
    }

    /**
     * 登录
     *
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    @RequestMapping("/login")
    public String login(String userName, String password) {
        // 获取主体对象
        Subject subject = SecurityUtils.getSubject();

        try {
            // 令牌
            UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
            subject.login(token);
            logger.info("登录成功！");
            return "redirect:/index.jsp";
        } catch (UnknownAccountException e) {
            logger.error("用户名错误！", e);
        } catch (IncorrectCredentialsException e) {
            logger.error("密码错误！", e);
        }
        // 登录有异常或错误，重新跳转至登录页面
        return "redirect:/login.jsp";
    }

    /**
     * 退出
     *
     * @return
     */
    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return "redirect:/login.jsp";
    }

    @RequestMapping("/getImage")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        // 生成验证码
        String code = VerifyCodeUtils.generateVerifyCode(4);
        // 验证码放入session
        session.setAttribute("code", code);
        logger.info("验证码：" + code);
        // 验证码存入图片
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("image/png");
        VerifyCodeUtils.outputImage(220, 60, os, code);
    }

}
